package sim.core;

import java.awt.*;

public class NetworkPacket extends Task {
      
    EndPoint src, dst ;
    private double delay ;
    private EncodedMessage payload ;
    
    public NetworkPacket( EndPoint src, EndPoint dst, Message payload, double delay ) {
    	super( delay ) ;
        this.src = src ;
        this.dst = dst ;
        this.delay = delay ;
        this.payload = payload.encode() ;
    }
    
    public double delay() {
        return delay ;
    }
    
    public int length() {
        return payload.length() + 28 ;
    }
    
    public void run() {
    	if( dst.online )
    		payload.decode().deliverTo( src, dst.handler) ;
    }
    
    public String toString() {
        return "NetworkPacket from:" + src + " to " +  dst ;
    }
    
    public void display( Graphics2D gu, Graphics2D gs) {
    	Message msg = payload.decode() ;
    	
    	if( msg.isVisible() )
    		msg.display( src, dst, gs) ;   	
    }
    
}
